// pages/myinfo/myinfo.js
const app = getApp()
const date = new Date()
const years = []
var months = []
var days = []
for (let i = 1900; i <= new Date().getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bankCard: app.globalData.bankCard,
    showSex: false,
    showBank: true,
    user: {},
    realName:'',
    bankName:'',
    encBankNo:'',
    weixinInfo:'',
    mail:"",
    sex:'',
    isOpen:false,
    isClick:false,
    ifNotice:false,
    viewUid:'',
    isOneSelf:true,
    iscompany:false,
    birthdayDefault: 0,
    isShowBirthday: 0,
    isShowBirthday1: 0,
    showBirthday: false,
    birthday: '',
    years: years,
    year: date.getFullYear(),
    months: months,
    month: 1,
    days: days,
    day: 1,
    value: [95, 0, 0],
    val: [95, 0, 0],
  },

  //姓名
  realNameBindtap:function(e) {
    if (e.detail.value.trim().length == 0) {
      this.setData({
        realName: ''
      })
    }else{
      this.setData({
        realName: e.detail.value.replace(/^\s+|\s+$/g,'')
      })
    }
    this.toggle("user.realName", this.data.realName);
  },
  //开户行
  bankNameBindtap:function(e) {
    this.setData({
      bankName: e.detail.value
    })
    this.toggle("user.bankName", this.data.bankName);
  },
// 生日
  showbirthday(){
    this.toggle("showBirthday", true)
  },
  bindChange: function (e) {
    const val = e.detail.value
    if(this.data.years[val[0]] >= new Date().getFullYear()){
      var months1 = [];
      var days1 = [];
      for (let i = 1; i <= new Date().getMonth() + 1; i++) {
        months1.push(i)
      }
      for (let i = 1; i <= new Date().getDate(); i++) {
        days1.push(i)
      }
      this.toggle("months", months1);
      this.toggle("days", days1);
    }else{
      this.toggle("months", months);
      this.toggle("days", days);
    }
    this.toggle("val", val)
  },
  ischoose(){
    if(this.data.isShowBirthday1==0){
      this.toggle("isShowBirthday1", 1)
    }else{
      this.toggle("isShowBirthday1", 0)
    }
  },
  bircancel(){
    this.toggle("showBirthday", false)
    if (this.data.isShowBirthday1 != this.data.isShowBirthday) {
      this.toggle("isShowBirthday1", this.data.isShowBirthday)
    }
  },
  birsure(){
    const val =this.data.val
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]],
      showBirthday:false,
      birthday: this.data.years[val[0]] + '-' + this.data.months[val[1]] + '-' +this.data.days[val[2]]+' 00:00:00'
    })
    if (this.data.isShowBirthday1 != this.data.isShowBirthday) {
      this.toggle("isShowBirthday", this.data.isShowBirthday1)
    }
  },
  //银行卡号
  encBankNoBindtap:function(e) {
    this.setData({
      encBankNo: e.detail.value
    })
    this.toggle("user.encBankNo", this.data.encBankNo);
  },

  //微信号
  weixinInfoBindtap:function(e) {
    if (e.detail.value.trim().length == 0) {
      this.setData({
        weixinInfo: ''
      })
    }else{
      this.setData({
        weixinInfo: e.detail.value
      })
    } 
    this.toggle("user.weixinInfo", this.data.weixinInfo);
  },
  
  //邮箱
  mailBindtap:function(e) {
    this.setData({
      mail: e.detail.value.trim()
    })
    this.toggle("user.mail", this.data.mail);
  },
//公司
  iscompanyfocus(){
    this.setData({
      iscompany:true,
    })
  },
  companyBindtap: function (e) {
    if (e.detail.value.trim().length == 0) {
      this.setData({
        company: ''
      })
    }else{
      this.setData({
        company: e.detail.value
      })
    }
    this.toggle("user.company", this.data.company);
  },
  companyblur(e){
    if (e.detail.value.trim().length == 0) {
      this.setData({
        company: '',
        iscompany: false,
      })
    } else {
      this.setData({
        company: e.detail.value,
        iscompany: false,
      })
    }
    this.toggle("user.company", this.data.company);
  },
//职务
  postBindtap: function (e) {
    if (e.detail.value.trim().length == 0) {
      this.setData({
        post: ''
      })
    }else{
      this.setData({
        post: e.detail.value
      })
    }
    this.toggle("user.post", this.data.post);
  },
//公司地址
  companyAddressBindtap: function (e) {
    if (e.detail.value.trim().length == 0) {
      this.setData({
        companyAddress: ''
      })
    }else{
      this.setData({
        companyAddress: e.detail.value
      })
    }
    this.toggle("user.companyAddress", this.data.companyAddress);
  },
  companyAddresblur(e){
    if (e.detail.value.trim().length == 0) {
      this.setData({
        companyAddress: '',
        ifNotice: false
      })
    }else{
      this.setData({
        companyAddress: e.detail.value,
        ifNotice: false
      })
    }
    this.toggle("user.companyAddress", this.data.companyAddress);
  },
  ifNotice() {
    this.setData({
      ifNotice: true
    })
  },
  saveInfo() {
    var that = this;
    if (that.data.isClick) {
      return
    }
    // var reg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/;
    var emailPattern = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
      if (that.data.realName == '') {
        wx.showToast({
          title: "请输入姓名",
          icon: 'none',
          duration: 2000
        })
        return;
      } else if (that.data.user.mail) {
      if (!emailPattern.test(that.data.user.mail)) {
        wx.showToast({
          title: "邮箱格式错误",
          icon: 'none',
          duration: 2000
        })
        return;
      }
    }
    that.setData({
      isClick:true
    })
    // that.toggle("user.sex", that.data.sex);
    
    app.getUserInfo(function (userInfo) {
      app.req({
        url: "wechat/user/updateUserInformation",
        data: {
          realName: that.data.user.realName,
          bankName: that.data.user.bankName,
          encBankNo: that.data.user.encBankNo,
          weixinInfo: that.data.user.weixinInfo,
          mail: that.data.user.mail,
          sex:that.data.user.sex,
          company: that.data.user.company,
          post: that.data.user.post,
          companyAddress: that.data.user.companyAddress,
          birthday: that.data.birthday,
          birthdayDefault:0,
          isShowBirthday: that.data.isShowBirthday,
        },
        method: "post",
        success: function (res) {
          if (200 == res.code) {
            wx.showToast({
              title: "保存成功",
              icon: 'none',
              duration: 2000
            })
            that.setData({
              isClick: false
            })
          } else {
            
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })

            that.setData({
              isClick: false
            })
          }
        }
      })
    });
  },

  clickBankCardInformation:function() {
    this.setData ({
      isOpen: this.data.isOpen == true ? false :true
    })
    console.log(this.data.isOpen)
  },

  onClose: function() {
    this.toggle("showSex", false);
  },
  showSexSelect: function() {
    this.toggle("showSex", true);
  },
  selectToItemSex: function(event) {
    var that = this;
    var data = event.currentTarget.dataset;
    that.toggle("user.sex", data.id);
    // that.saveSex(data.id);
    that.setData({
      sex: data.id
    })
    that.toggle("showSex", false);
  },
  
  editItem: function(event) {
    var data = event.currentTarget.dataset;
    var param = "key=" + data.key;
    param += "&value=" + data.value || "";
    param += "&max=" + data.max;
    param += "&type=" + data.type;
    param += "&label=" + data.label;
    wx.navigateTo({
      url: '/pages/myinfoinput/myinfoinput?' + param
    });
    console.log(data.label)
  },
  toggle: function(type, val) {
    this.setData({
      [type]: val
    });
  },
  tapShowBank(){
    this.setData({
      showBank: !this.data.showBank
    })
  },


  changeAvatar: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        const filepath = res.tempFilePaths[0]
        const src = filepath
        console.log("原地址", src)
        wx.navigateTo({
          url: `../upload/upload?src=${src}`
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    if (options.viewUid == undefined) {
      that.setData({
        isOneSelf: true
      })
    } else {
      that.setData({
        isOneSelf: false,
        viewUid: options.viewUid
      })
    }
    console.log("id", that.data.viewUid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    app.getUserInfo(function(userInfo) {
      // that.setData({
      //   user: userInfo,
      //   realName: userInfo.realName,
      //   bankName: userInfo.bankName,
      //   encBankNo: userInfo.encBankNo,
      //   sex: userInfo.sex,
      //   weixinInfo: userInfo.weixinInfo,
      //   mail: userInfo.mail
      // });
      
    });
    that.getUserInfo()
    // console.log("user", that.data.user)
  },


  getUserInfo: function () {
    var that = this;
    var data = {}
    var URL = ''
    if (!that.data.isOneSelf) {
      // data ={
      //   "viewUid": that.data.viewUid,
      // }
      URL = "wechat/user/getOtherInformation?&viewUid=" + that.data.viewUid
    } else {
      URL = "wechat/user/getUserInformation"
    }
    app.req({
      url: URL,
      method: "post",
      // data: data,
      success: function (res) {
        if (200 == res.code) {
          that.setData({
            user: res.data,
            birthday:res.data.birthday,
            birthdayDefault:res.data.birthdayDefault,
            year: res.data.birthday.substring(0, 4),
            month: res.data.birthday.substring(5, 7),
            day: res.data.birthday.substring(8, 10),
            isShowBirthday: res.data.isShowBirthday,
            isShowBirthday1: res.data.isShowBirthday
          });
          if (that.data.user.realName==''){
            that.setData({
              realName: that.data.user.nickName
            })
          }else{
            that.setData({
              realName: that.data.user.realName
            })
          }
          console.log("user", that.data.user)
        } 
      }
    });
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  }
})